using System;
using RollManager.Forms.MainPanels;
using RollManager.Library;

namespace RollManager.Forms.MenuDialogs
{
    public partial class CopySubActionDialog : MenuDialog
    {
        private SubAction m_subAction;
        private bool m_moveSubAction;

        private RollManager.Library.Action Action
        {
            get
            {
                return (RollManager.Library.Action) actionList.SelectedItem;
            }
        }

        private DevLevel DevLevel
        {
            get
            {
                return (DevLevel) devLevelList.SelectedItem;
            }
        }

        private ServerGroup ServerGroup
        {
            get
            {
                return (ServerGroup) serverGroupList.SelectedItem;
            }
        }

        public CopySubActionDialog( AdminActionsPanel panel, SubAction subAction, bool move )
            : base( panel )
        {
            m_subAction = subAction;
            m_moveSubAction = move;
            InitializeComponent();

            nameBox.Text = m_subAction.Name;

            devLevelList.DisplayMember = ItemBase.NAME_PROPERTY;
            devLevelList.DataSource = Database.GetDevLevels();
            RollManager.Library.Action curAction = Database.GetAction( m_subAction.ActionId );
            foreach( DevLevel devLevel in devLevelList.Items )
            {
                if( devLevel.Id == curAction.DevLevelId )
                {
                    devLevelList.SelectedItem = devLevel;
                    break;
                }
            }

            actionList.DisplayMember = ItemBase.NAME_PROPERTY;
            actionList.DataSource = Database.GetActions( DevLevel.Id );
            foreach( RollManager.Library.Action action in actionList.Items )
            {
                if( action.Id == m_subAction.ActionId )
                {
                    actionList.SelectedItem = action;
                    break;
                }
            }

            serverGroupList.DisplayMember = ItemBase.NAME_PROPERTY;
            serverGroupList.DataSource = Database.GetServerGroups();
            foreach( ServerGroup group in serverGroupList.Items )
            {
                if( group.Id == m_subAction.ServerGroupId )
                {
                    serverGroupList.SelectedItem = group;
                    break;
                }
            }
        }

        private void okButton_Click( object sender, EventArgs e )
        {
            Database.CopySubAction( m_subAction.Id, nameBox.Text, Action.Id, ServerGroup.Id );

            if( m_moveSubAction )
            {
                Database.DeleteSubAction( m_subAction.Id );
            }

            CloseAndRefresh();
        }

        private void cancelButton_Click( object sender, EventArgs e )
        {
            Close();
        }

        private void devLevelList_SelectionChangeCommitted( object sender, EventArgs e )
        {
            actionList.DataSource = Database.GetActions( DevLevel.Id );
        }
    }
}